//用户 API 服务
import '../model/api_response.dart';
import '../model/post.dart';
import '../util/http_client.dart';
import '../util/parser_utils.dart';

class PostApi {
  static Future<ApiResponse<List<Post>>> getPosts() async {
    try {
      final response = await HttpClient.get("/posts");
      return ParserUtils.parseListResponse<Post>(
        response,
        itemParser: (json) => Post.fromJson(json),
      );
    } catch (e) {
      return ApiResponse(
          data: null, status: 500, message: "Failed to get posts: $e");
    }
  }

  static Future<ApiResponse<Post>> createUser(Post user) async{
    try{
      final userData = user.toJson()..remove('id');
      final response = await HttpClient.post("/posts",body: userData);

      return ParserUtils.parseObjectResponse<Post>(response, parser: (json)=>Post.fromJson(json));

    }catch(e){
      return ApiResponse(data: null, status: 500, message:  "Failed to create Post: $e",);
    }
  }
}
